# from sqlalchemy.future import create_engine
# from sqlalchemy.orm import sessionmaker
from seldom.db_operation import MySQLDB
from utils.conf_handler import ConfHandler
# 使用sqlalchemy时兼容Mac
import pymysql
pymysql.install_as_MySQLdb()


class QueryObjectData():

    def __init__(self):
        conf = ConfHandler()
        my_conf = conf.conf_handler("mysql")
        self.db = MySQLDB(host=my_conf.get('host'),
                          port=my_conf.get('port'),
                          user=my_conf.get('user'),
                          password=my_conf.get('password'),
                          database=my_conf.get('database'))

    # def query_one_data(self, obj, condition_name, condition_value):
    #     """
    #     :param obj: 数据库的映射对象
    #     :param condition_name:  查询条件的名称
    #     :param condition_value:  查询条件名称对应的值
    #     :return:
    #     """
    #     session = Session()
    #     query = session.query(obj)
    #     result = query.filter(condition_name == condition_value).one()
    #     session.close()
    #     return result
    #
    # def query_all_data(self, obj, condition_name, condition_value):
    #     """
    #     :param obj: 数据库的映射对象
    #     :param condition_name:  查询条件的名称
    #     :param condition_value:  查询条件名称对应的值
    #     :return:
    #     """
    #     session = Session()
    #     query = session.query(obj)
    #     result = query.filter(condition_name == condition_value).all()
    #     session.close()
    #     return result

    #    查一条数据
    def select_one_data(self, table, where):
        data = self.db.select_data(table=table, where=where)
        self.db.close()
        return data

    #    根据sql查询
    def query_sql(self, sql):
        data = self.db.query_sql(sql=sql)
        self.db.close()
        print(data)

if __name__ == '__main__':
    get=QueryObjectData()
    get.query_sql(sql = 'select * from bwos_brand_info where id = "1816014236551786498"')
